import pandas as pd
from pandas import DataFrame, Series

import tools

pd.set_option('display.unicode.east_asian_width', True)

def get_score(path):
    filenames = tools.get_filenames(path)
    tools.检查路径名(filenames)
    ids = []
    names = []
    scores = []
    for filename in filenames:
        if len(filename.split('.')) < 4:
            print(filename)
            exit(0)

        id = filename.split('.')[0].strip('-').split('-')[1]
        name = filename.split('.')[0].strip('-').split('-')[2]
        score = filename.split('.')[2]
        # print(f'{id} {name} {score}')
        ids.append(id)
        # names.append(name)
        scores.append(score)

    df_new = pd.DataFrame({'学号': ids, path.split('\\')[-1]: scores})
    # df_new.set_index('学号', inplace=True)

    return df_new

def get_name_date(docx_dir:str) -> DataFrame:
    filenames = tools.get_filenames(docx_dir)
    tools.检查路径名(filenames)
    names = []
    dates = []
    for filename in filenames:
        name, date = tools.truncate_name_date(filename)
        names.append(name)
        dates.append(date)

    return pd.DataFrame(list(zip(names, dates)), columns=['姓名', '日期'])

def 统计重复(path):
    filenames = tools.get_filenames(path)
    tools.检查路径名(filenames)
    ids = []
    names = []
    scores = []
    for filename in filenames:
        if len(filename.split('.')) < 3:
            print(filename)
            exit(0)

        name = filename.split('.')[0].strip('-').split('-')[2]
        names.append(name)

    se = pd.Series(names)
    print(se[se.duplicated()])
    return len(se[se.duplicated()])

"""
1223	10
1224	7
1225	7
1226	4
1227	4
1228	3
1230	3
0103	1
0109	1
"""

def calc_score(date):
    match date:
        case '1223':
            return 10
        case '1224':
            return 7
        case '1225':
            return 7
        case '1226':
            return 4
        case '1227':
            return 4
        case '1228':
            return 3
        case '1230':
            return 3
        case '0103':
            return 1
        case '0109':
            return 1

def date_to_score(dates: Series):
    return dates.apply(calc_score)

if __name__ == '__main__':
    base_path = r"D:\100-Project\2024-2025-1学期安排\武昌首义学院\29101302_《通信数据分析技术实训（企业）》\实训报告"
    docx_dir = base_path + r"\提交"
    excel_file = base_path + r"\成绩提取.xlsx"

    if 统计重复(docx_dir) != 0:
        exit(0)

    print(tools.统计时间信息(docx_dir).values)

    df0 = pd.read_excel(base_path + r"\《数据分析实训》报告提交记录.xlsx")
    df0 = df0[['学号', '姓名']]

    df1 = get_name_date(docx_dir)

    # df0['学号'] = df0['学号'].astype(str)
    # df1['学号'] = df1['学号'].astype(str)
    df3 = pd.merge(df0, df1, on="姓名", how='outer')
    df3.sort_values(by='学号', ignore_index=True, inplace=True)
    df3['日期'] = date_to_score(df3['日期'])
    df3.fillna(0,inplace=True)
    print(df3)
    df3.to_excel(r"D:\tmp_2.xlsx")

"""
    df = pd.read_excel(base_path + r"\《数据分析实训》报告提交记录.xlsx", header=0)
    df.fillna(0, inplace=True)
    df = df.iloc[:, 0:2]
    df = df.astype(str)
    df = pd.merge(df, df3, on='学号', how='outer')
"""